package sh.wn36.vehiclemanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.entity.*;
import sh.wn36.vehiclemanage.entity.dto.InspectionQuery;
import sh.wn36.vehiclemanage.entity.qz_entity.InspectionRecordList;
import sh.wn36.vehiclemanage.entity.qz_entity.Users;
import sh.wn36.vehiclemanage.mapper.CurrentUnitMapper;
import sh.wn36.vehiclemanage.service.*;
import sh.wn36.vehiclemanage.util.Result;
import sh.wn36.vehiclemanage.xzy.Query;
import sh.wn36.vehiclemanage.xzy.UnitQuery;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */

@CrossOrigin
@RestController
@RequestMapping("/inspection-record")
public class InspectionRecordController {
    @Autowired
    @ApiModelProperty("年检接口")
    private InspectionRecordListService inspectionRecordListService;
    @ApiModelProperty("车管所的接口")
    @Autowired
    private CurrentUnitService currentUnitService;
    @ApiModelProperty("汽车接口")
    @Autowired
    private CarService carService;

    @Autowired
    @ApiModelProperty("司机接口")
    private DriverService driverService;
    @Autowired
    @ApiModelProperty("保存和修改年检")
    private InspectionRecordService inspectionRecordService;


    @ApiOperation("根据条件分页查询")
    @PostMapping("/pageInspection/{current}/{limit}")
    public Result pageDriverByConditon(@ApiParam(value = "当前页码", required = true, name = "currentPage") @PathVariable("current") Integer current,
                                       @ApiParam(value = "当前页数", required = true, name = "pageSize") @PathVariable("limit") Integer limit,
                                       @ApiParam(value = "查询条件", required = false, name = "carno") @RequestBody(required = false) InspectionQuery query) {
        Page<InspectionRecordList> inspectionRecordListPage = new Page<>(current, limit);
        QueryWrapper<InspectionRecordList> queryWrapper = new QueryWrapper<>();
        System.out.printf("条件" + query.getCarNo());
        if (query != null) {
            String carNo = query.getCarNo();
            if (!StringUtils.isEmpty(carNo)) {
                queryWrapper.like("c.car_no", carNo);
            }
        }
        inspectionRecordListService.pageWrapper(inspectionRecordListPage, queryWrapper);
        //分页查询的方法
        //-- 实际返回的只有总记录数和具体的数据集合
        long total = inspectionRecordListPage.getTotal();
        List<InspectionRecordList> inspectionRecordLists = inspectionRecordListPage.getRecords();
        return Result.OK().data("total", total).data("items", inspectionRecordLists);

    }

    //    年检记录初始化页面加载
    @PostMapping("/inspectionInit")
    public Result inspectionInit() {
        List<CurrentUnit> type = currentUnitService.findType(92);
        List<Car> carslist = carService.list();
        List<Driver> driverslist = driverService.list();
        return Result.OK().data("vehicle", type).data("car", carslist).data("driver", driverslist);
    }


    @ApiModelProperty("年检记录添加")
    @PostMapping("/savevInspection")
    public Result savevInspection(@RequestBody InspectionRecord inspectionRecord) {
        boolean save = inspectionRecordService.save(inspectionRecord);
        if (save) {
            return Result.OK();
        }
        return Result.Fail();
    }

    //        按照年检id删除多个
    @GetMapping("/removeInspection/{id}")
    public Result removeInspection(@ApiParam(value = "年检记录ID", required = true, name = "id") @PathVariable("id") String id) {
        boolean result = inspectionRecordService.removeById(id);
        if (result) {
            return Result.OK();
        } else {
            return Result.Fail();
        }

    }

    //        按照id查询年检记录
    @GetMapping("/findByIdInspection/{id}")
    public Result findByIdInspection(@ApiParam(value = "记录ID", required = true, name = "id") @PathVariable("id") String id) {
        InspectionRecord inspectionRecord = inspectionRecordService.getById(id);
        Car car = carService.getById(inspectionRecord.getCarId());
        return Result.OK().data("inspection",inspectionRecord).data("car",car);
    }

    @PostMapping("/updateInspection")
    public Result updateInspection(@RequestBody InspectionRecord inspectionRecord){
        boolean updateById = inspectionRecordService.updateById(inspectionRecord);
        if(updateById){
            return  Result.OK();
        }
        return Result.Fail();
    }
}

